package com.dilidili.controller;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.dilidili.service.FileUpLoadService;
import com.dilidili.utils.FileUploadResult;
import com.aliyun.oss.model.OSSObjectSummary;
import com.dilidili.utils.VideoUtil;
import com.util.TransferUtil;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.MultimediaInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import ws.schild.jave.FfmpegFileInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

import static com.dilidili.utils.VideoUtil.getVideoPicture;

/**
 * <p>Title:阿里云oss存储 controller </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2020</p>
 * <p>Company: http://www.ubisys.com.cn/</p>
 *
 * @Auther: cw
 * @Date: 2020/9/4 21:31
 */
@Controller
@Slf4j
public class FileUploadController {

    @Autowired
    private FileUpLoadService fileUploadService;


    /**
     * 文件上传到oss
     *
     * @param uploadFile
     * @return
     * @throws Exception
     */
    @RequestMapping("file/upload")
    @ResponseBody
    public Object upload(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request) throws Exception {

        return fileUploadService.a(uploadFile,request);
    }
    @RequestMapping("file/upload2")
    @ResponseBody
    public Object upload2(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request) throws Exception {

        return fileUploadService.upload(uploadFile);
    }

//
//
/////获取视频时长
//    public static void main(String[] args) {
//        long urlVideoSecond = new FileUploadController().getURLVideoSecond(
//                "C:\\Users\\31444\\AppData\\Local\\Temp\\tomcat-docbase.9900.6659493025458641831\\business\\shops\\upload\\2022\\04\\04\\14\\3af54b8b-b4fc-49e4-9f33-64cd36120dfc.mp4");
//        System.out.println(urlVideoSecond+"1111");
//    }



    /**
     * 根据文件名删除
     *
     * @param objectName
     * @return
     * @throws Exception
     */
    @RequestMapping("file/delete")
    @ResponseBody
    public FileUploadResult delete(@RequestParam("fileName") String objectName)
            throws Exception {
        return fileUploadService.delete(objectName);
    }


    /**
     * 查看桶内所有文件
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("file/list")
    @ResponseBody
    public List<OSSObjectSummary> list()
            throws Exception {
        return fileUploadService.list();
    }

    /**
     * 根据文件名进行下载
     *
     * @param objectName
     * @param response
     * @throws IOException
     */
    @RequestMapping("file/download")
    @ResponseBody
    public void download(@RequestParam("fileName") String objectName, HttpServletResponse response) throws IOException {
        //通知浏览器以附件形式下载
        response.setHeader("Content-Disposition",
                "attachment;filename=" + new String(objectName.getBytes(), "ISO-8859-1"));
        fileUploadService.exportOssFile(response.getOutputStream(), objectName);
    }


}